﻿using System;
using UnityEngine;

public class TurnBaseManager : SingletonMono<TurnBaseManager>
{
    private bool player1TurnEnd = false;
    private bool player1TurnBegin = false;
    private bool player2TurnBegin = false;  
    private bool gameStart = false;


    private void Update()
    {
        if(!gameStart)
            return;

        if (player1TurnEnd)
        {
            BeforePlayer1TurnEnd();
        }

        if (player1TurnBegin)
        {
            BeforePlayer1TurnBegin();
        }

        if (player2TurnBegin)
        {
                BeforePlayer2TurnBegin();
        }
        
    }

    private void OnEnable()
    {
        EventCenter.GetInstance().AddEventListener(nameof(Player1TurnEnd), Player1TurnEnd);
        EventCenter.GetInstance().AddEventListener(nameof(Player2TurnEnd), Player2TurnEnd);
    }

    private void OnDisable()
    {
        EventCenter.GetInstance().RemoveEventListener(nameof(Player1TurnEnd), Player1TurnEnd);
        EventCenter.GetInstance().RemoveEventListener(nameof(Player2TurnEnd), Player2TurnEnd);
    }

    
    public void StartBattle()
    {
        gameStart = true;
        Settings.isPlayerTurn = true;
        player1TurnBegin = false;
        player2TurnBegin = false;
        player1TurnEnd = false;
        BeforePlayer1TurnBegin();
    }

    public void gameOver()
    {
        gameStart = false;
        player1TurnBegin = false;
        player2TurnBegin = false;
        player1TurnEnd = false;
        Settings.isPlayerTurn = false;
    }

    //在玩家回合开始之前
    public void BeforePlayer1TurnBegin()
    {
        player1TurnBegin = false;
        EventCenter.GetInstance().EventTrigger(nameof(BeforePlayer1TurnBegin));
        EventCenter.GetInstance().EventTrigger<GameEvent>(SkillEventName.EventTurnChange.ToString(),new TurnChangeEvent(ChessSide.Player1));
        EventCenter.GetInstance().EventTrigger("AddResources",ChessSide.Player1,1);
        if(gameStart)
            AfterPlayer1TurnBegin();
    }
    
    //在玩家回合开始之后
    public void AfterPlayer1TurnBegin()
    {
        EventCenter.GetInstance().EventTrigger(nameof(AfterPlayer1TurnBegin));
        EventCenter.GetInstance().EventTrigger<int,ChessSide>("DrawCard",2,ChessSide.Player1);
    }

    [ContextMenu("Player1TurnEnd")]
    public void Player1TurnEnd()
    {
        player1TurnEnd = true;
    }
    
    //在玩家回合结束之前
    public void BeforePlayer1TurnEnd()
    {
        Settings.isPlayerTurn = false;
        player1TurnEnd = false;
        EventCenter.GetInstance().EventTrigger(nameof(BeforePlayer1TurnEnd));
        if (gameStart)
            AfterPlayer1TurnEnd();
    }
    
    //在玩家回合结束之后
    public void AfterPlayer1TurnEnd()
    {
        EventCenter.GetInstance().EventTrigger(nameof(AfterPlayer1TurnEnd));
        Settings.isPlayerTurn = false;
        player2TurnBegin = true;
    }
    
    //在敌方回合开始之前
    public void BeforePlayer2TurnBegin()
    {
        player2TurnBegin = false;
        EventCenter.GetInstance().EventTrigger(nameof(BeforePlayer2TurnBegin));
        EventCenter.GetInstance().EventTrigger<GameEvent>(SkillEventName.EventTurnChange.ToString(),new TurnChangeEvent(ChessSide.Player2));
        EventCenter.GetInstance().EventTrigger("AddResources",ChessSide.Player2,1);
        if (gameStart)
            AfterPlayer2TurnBegin();
    }
    

    //在敌方回合开始之后
    public void AfterPlayer2TurnBegin()
    {
        EventCenter.GetInstance().EventTrigger(nameof(AfterPlayer2TurnBegin));
        EventCenter.GetInstance().EventTrigger<int, ChessSide>("DrawCard", 2, ChessSide.Player2);

        
    }
    
    public void Player2TurnEnd()
    {
        if (gameStart)
            BeforePlayer2TurnEnd();
    }
    
    //在敌方回合结束之前
    public void BeforePlayer2TurnEnd()
    {
        Settings.isPlayerTurn = true;
        EventCenter.GetInstance().EventTrigger(nameof(BeforePlayer2TurnEnd));
        if (gameStart)
            AfterPlayer2TurnEnd();
    }
    
    //在敌方回合结束之后
    public void AfterPlayer2TurnEnd()
    {
        EventCenter.GetInstance().EventTrigger(nameof(AfterPlayer2TurnEnd));
        player1TurnBegin = true;
    }
}
